package com.jeecms.cms.manager.main.impl;

import com.jeecms.common.hibernate4.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.cms.dao.main.CmsEnterpriseDao;
import com.jeecms.cms.entity.assist.CmsEnterprise;
import com.jeecms.cms.manager.main.CmsEnterpriseMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Hashan on 2018-02-03.
 *
 */
@Service
@Transactional(readOnly = true)
public class CmsEnterpriseMngImpl implements CmsEnterpriseMng {
    @Override
    public CmsEnterprise findById(Integer id) {
        return cmsEnterpriseDao.findById(id);
    }

    @Transactional(readOnly = false)
    @Override
    public CmsEnterprise save(CmsEnterprise bean) {
        return cmsEnterpriseDao.save(bean);
    }

    @Transactional(readOnly = false)
    @Override
    public CmsEnterprise update(CmsEnterprise cmsEnterprise) {
        Updater<CmsEnterprise> updater = new Updater<CmsEnterprise> (cmsEnterprise);
        return cmsEnterpriseDao.updateByUpdater(updater);
    }

    @Transactional(readOnly = false)
    @Override
    public CmsEnterprise deleteById(Integer id) {
        /*unifiedUserMng.deleteById(id);
        CmsUser bean = cmsUserDao.deleteById(id);
        //删除收藏信息
        bean.clearCollection();
        Set<Channel> channels = bean.getChannels();
        // 清除
        for (Channel channel : channels) {
            channel.getUsers().remove(bean);
        }*/
        return cmsEnterpriseDao.deleteById(id);
    }

    @Override
    public List<CmsEnterprise> getList(Map<String, Object> params) {
        return cmsEnterpriseDao.getList(params);
    }

    @Override
    public Pagination getPage(Map<String, Object> params) {
        return cmsEnterpriseDao.getPage(params);
    }

    @Transactional(readOnly = false)
    @Override
    public CmsEnterprise[] deleteByIds(Integer[] ids) {
        CmsEnterprise[] beans = new CmsEnterprise[ids.length];
        for (int i = 0, len = ids.length; i < len; i++) {

            beans[i] = deleteById(ids[i]);
        }
        return beans;
    }

    @Override
    public CmsEnterprise getEnterpriseByInvitationCode(String invitationCode) {
        return cmsEnterpriseDao.getEnterpriseByInvitationCode(invitationCode);
    }

    @Autowired
    private CmsEnterpriseDao cmsEnterpriseDao;

    @Override
    public List<Object[]> findBranchTreeForEn(Integer enterpriseId) {
        return cmsEnterpriseDao.findBranchTreeForEn(enterpriseId);
    }
}
